<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px" @keyup.enter="submitHandle()">
			<el-form-item label="值班时间" prop="workTime">
				<el-date-picker
					v-model="dataForm.workTime"
					format="YYYY-MM-DD"
					type="datetime"
				/>
			</el-form-item>
			<el-form-item label="值班" prop="isDayName">
				<el-select v-model="dataForm.isDayName">
					<el-option
					v-for="item in dataForm.daylist"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item prop="doctorName" class="divlineleft" label="姓名：">
				<el-input v-model="dataForm.doctorName" @click="addUserHandle()"></el-input>
			</el-form-item>
			<el-form-item prop="deptName" class="divlineleft" label="部门：">
				<el-input v-model="dataForm.deptName"  @click="addOrUpdateHandle()"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	<!-- 弹窗, 新增 / 修改 -->
	<datalistUser ref="datalistUserRef" @fatherMethod="fatherMethod"></datalistUser>
	<!-- 弹窗, 新增 / 修改 -->
	<listUser ref="listUserRef" @fatherUserMethod="fatherUserMethod"></listUser>
	<!-- 弹窗, 新增 / 修改 -->
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useDoctorWorkApi, useDoctorWorkSubmitApi } from '@/api/sys/doctorwork'

import datalistUser from './datalist.vue'
import listUser from './userlist.vue'
import { el } from 'element-plus/es/locale'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	doctorId: '',
	doctorName: '',
	deptName: '',
	deptId: '',
	workTime:'',
	isDay:0,
	isDayName:'',
	orgId:'',
	daylist:[
		{
			value: 0,
    		label: '上午',
		},
		{
			value: 1,
    		label: '下午',
		}
	],
})
const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''
	let orgid:any=localStorage.getItem('org');
	dataForm.orgId=orgid;
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	if (id) {
		getPost(id)
	}
}
// id 存在则为修改


const getPost = (id: number) => {
	useDoctorWorkApi(id).then(res => {
		if(dataForm.isDay==0){
			dataForm.isDayName='上午'
		}else{
			dataForm.isDayName='下午'
		}
		Object.assign(dataForm, res.data)
	})
}
const datalistUserRef = ref()
const addOrUpdateHandle = () => {
	datalistUserRef.value.init();
}
const fatherMethod=(orgTit:any,org:any)=>{
	dataForm.deptName=orgTit;
	dataForm.deptId=org;
}

const listUserRef = ref()
const addUserHandle = () => {
	listUserRef.value.init();
}

const fatherUserMethod=(realName:any,uid:any)=>{
	dataForm.doctorName=realName;
	dataForm.doctorId=uid;
}

const dataRules = ref({
	doctorName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	deptName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		if(dataForm.isDayName=='上午'){
			dataForm.isDay=0;
		}else{
			dataForm.isDay=1;
		}
		useDoctorWorkSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
